var path = require('path');
var log4js = require(path.resolve(__dirname, '../config/logger'));
var logger = log4js.getLogger('status');
var DbCommon = require('./db');
const Article = require('./article');
class ArticleCategory {
  constructor(id) {
    this.id = id;
    this.name = '';
    this.template = '';
    this.contents = [];
    this.title = [];
    this.category = 1;
    this.channel = 1;
    this.publish_date = 0;
    this.publish_day = "";
    this.publish_time = '';
    this.status = 1;
    this.updater = '';
  }
  init() {
    let that = this;
    return new Promise(function (resolve, reject) {
      let wheres = [{
        type: 1,
        key: 'id',
        value: that.id
      }]
      let sqlOptions = {
        wheres: wheres,
        table: 'article'
      }
      DbCommon.getDataById(sqlOptions).then(function (data) {
        if (data.length > 0) {
          that.name = data[0].name;
          that.status = data[0].status;
          that.category = data[0].category;
          that.channel = data[0].channel;
          that.publish_date = data[0].publish_date;
          that.publish_day = data[0].publish_day;
          that.publish_time = data[0].publish_time;
          that.updater = data[0].updater || '';
          that.template = JSON.parse(data[0].template);
          that.title = that.template.title;
          that.contents = that.template.contents;
          resolve();
        } else {
          reject(new Error('no data find'));
        }
      }).catch(function (error) {
        reject(error);
      });
    })
  }
  generateContent() {
    let that = this;
    return new Promise(function (resolve, reject) {
      that.init().then(function () {
        //let dtcode = '0101600499';
        let article = new Article({
          id: that.id,
          name: that.name,
          channel: that.channel,
          template: that.template,
          title: that.title,
          contents: that.contents,
          status: that.status,
          autoParams: {}
        })
        article.compile().then(function (res) {
            //console.log(article.content);
            resolve();
          })
          .catch(function (error) {
            reject(error);
          });
      }).catch(function (error) {
        reject(error);
      });
    })

  }
  static preview(options) {
    let that = this;
    return new Promise(function (resolve, reject) {
      let template = JSON.parse(options.template);
      let title = template.title;
      let contents = template.contents;
      let obj = {
        id: null,
        name: options.name || '',
        channel: options.channel || 1,
        template: template,
        title: title,
        contents: contents,
        status: 1,
        autoParams: {}
      };
      let article = new Article(obj);
      article.compile(obj).then(function (res) {
//      	console.log(article.content);
          resolve(article.content);
        })
        .catch(function (error) {
          reject(error);
        });

    })
  }
  // static previewSingleStock(options) {
  //   let that = this;
  //   return new Promise(function(resolve, reject) {

  //   })
  // }

}
module.exports = ArticleCategory;